#!usr/bin/node

class Dog{
  constructor(name,energy) {
    this.#name = name;
    this.#energy = energy;

    const timer = setInterval(()=>{
      if(this.#energy < 0) {
        clearInterval(timer);
        return ;
      }else{
        this.emit("bark",{
          name:this.#name,
          energy:this.#energy--});
      }
    },1000)
  }

  //订阅事件
  on(eventName,lisenter) {
    if(typeof this.#lisenters[eventName] === 'undefined') {
      this.#lisenters[eventName] = [];
    }
    this.#lisenters[eventName].push(lisenter);
  }

  //发出事件
  emit(eventName,argument) {
    this.#lisenters[eventName].forEach((fn)=>{
      fn.call(this,argument);
    });
  }

#lisenters = {};
  #name = '';
  #energy = 0;
}

module.exports = Dog;
